package com.xing.serviceedu.service.impl;

import com.mysql.cj.protocol.PacketReceivedTimeHolder;
import com.xing.servicebase.exceptionhandler.GuliException;
import com.xing.serviceedu.entity.EduCourse;
import com.xing.serviceedu.entity.EduCourseDescription;
import com.xing.serviceedu.entity.vo.CourseInfoVo;
import com.xing.serviceedu.entity.vo.CoursePublishVo;
import com.xing.serviceedu.entity.vo.CourseWebVo;
import com.xing.serviceedu.mapper.EduCourseMapper;
import com.xing.serviceedu.service.EduChapterService;
import com.xing.serviceedu.service.EduCourseDescriptionService;
import com.xing.serviceedu.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xing.serviceedu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.spi.CharsetProvider;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author xingfuao
 * @since 2022-01-09
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService chapterService;



    /**
     * 根据课程id查询课程基本信息
     * @param courseId
     * @return
     */
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //1 查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse, courseInfoVo);

        //2 查询描述表
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(courseDescription.getDescription());
        return courseInfoVo;
    }

    /**
     * 修改课程信息
     * @param courseInfoVo
     */
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) throws GuliException {

        //1.修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int update = baseMapper.updateById(eduCourse);

        if(update==0){
            throw new GuliException(20001,"修改课程信息失败");
        }

        //2.修改描述表
        EduCourseDescription description = new EduCourseDescription();
        description.setId(courseInfoVo.getId());
        description.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.updateById(description);

    }

    /**
     * 根据课程id查询课程信息
     * @param id
     * @return
     */
    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        //调用mapper
        CoursePublishVo publicCourseInfo = baseMapper.getPublicCourseInfo(id);
        return publicCourseInfo;
    }

    /**
     * 删除课程
     * @param courseId
     */
    @Override
    public void removeCourse(String courseId) {

        //1.根据课程id删除小节
        eduVideoService.removeVideoByCourseId(courseId);
        //2.根据课程id删除章节
        chapterService.removeChapterByCourseId(courseId);
        //3.根据课程id删除描述
        courseDescriptionService.removeById(courseId);
        //4.根据课程id删除课程本身
        int result = baseMapper.deleteById(courseId);
        if(result==0){  //失败返回
            throw new GuliException(20001,"删除失败");
        }
    }

    /**
     * 根据课程id获取课程信息
     * @param courseId
     * @return
     */
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        CourseWebVo baseCourseInfo = baseMapper.getBaseCourse(courseId);
        return baseCourseInfo;
    }
}
